package com.dbsop.domain.params;

import com.dbsop.common.util.EsFieldUtils;
import com.dbsop.domain.EsAggCondition;
import com.dbsop.domain.info.EsFiledInfo;
import com.dbsop.domain.info.Params;
import com.dbsop.mapper.IndexMappingInfo;
import com.dbsop.wrapper.DataWrapper;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;


@Getter
@Setter
public class AggRequestParams<T> implements Params {

    private EsAggCondition esAggCondition = EsAggCondition.getInstance();

    private Class<T> resClass;

    private IndexMappingInfo indexInfo;

    private DataWrapper<T> dataWrapper;

    public AggRequestParams(EsAggCondition esAggCondition, Class<T> resClass, IndexMappingInfo indexInfo) {
        this.esAggCondition = esAggCondition;
        this.resClass = resClass;
        this.indexInfo = indexInfo;
        indexHandler();
    }

    public AggRequestParams(EsAggCondition esAggCondition, IndexMappingInfo indexInfo, DataWrapper<T> dataWrapper) {
        this.esAggCondition = esAggCondition;
        this.indexInfo = indexInfo;
        this.dataWrapper = dataWrapper;
        indexHandler();
    }


    public Map<String, EsFiledInfo> getFieldMapping(){
        if (resClass!=null){
            return  EsFieldUtils.pareFieldEsInfo(resClass);
        }
       return null;
    }


    @Override
    public String[] getIndex() {
        return  esAggCondition.getCondition().getIndices();
    }

    @Override
    public void indexHandler() {
        String[] indices = esAggCondition.getCondition().getIndices();
        if (indices !=null && indices.length>0){
            return ;
        }
        esAggCondition.getCondition().addIndex(indexInfo.queryIndex());
    }
}
